Repository navigation
Stop native memory leak of ImagickDraw::setFont() in text rendering - #1562
Open
javiermarinros wants to merge 1 commit into
Open
javiermarinros wants to merge 1 commit into
javiermarinros wants to merge 1 commit into
Conversation
Contributor
Author
|
Reproduction, runs in a clean git clone https://gist.github.com/b1d2b55b14e47ebe412e2b940c3337e4.git repro && cd repro
docker run --rm -v "$PWD":/repro php:8.3-cli sh /repro/run.shIt installs imagick, clones |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ImagickDraw::setFont()leaks native memory on every call in imagick 3.8.1 and earlier: the extensionchecks the font against
MagickQueryFonts()and frees the list but not the font names in it(fix pending in Imagick/imagick#786, open since May 2026). The Imagick
FontProcessorcreates a newImagickDrawand callssetFont()for every measure and every line, several times pertext()call,and the driver returned a new
FontProcessoreach time.This PR:
FontProcessorper Imagick driver;ImagickDraw, and clones it intoImagickDraw().A clone copies the font without calling
setFont()again. Font size and color are still set on the clone.Native memory (RSS) growth over 2000 images with 3 texts each, after warm-up:
The loss grows with the number of fonts installed on the system. Time per image also drops about 10 %
(4.56 → 4.11 ms on IM 7). Rendered output is byte-identical to
develop(PNG and JPEG, 18 cases: 3 sizes,3 alignments, 2 angles, wrap, line height, stroke, 2 texts per image) on all three versions.
The cache is keyed by the real path of the font file, so a relative path keeps working after
chdir().Notes for review:
from the drawing call instead of
toImagickDraw().ImagickDrawper font file for its lifetime.ImagickDrawobjects, which PHP cannot serialize. The librarynever serializes drivers.
Tests:
FontProcessorreturns independent draw objects with their own size and color; the driverreturns the same font processor. Full suite: same result as
developon IM 7.1.1 and 6.9.11 (theremaining failures come from missing exif/heic support in my environment). phpstan and phpcs pass.
🤖 Generated with Claude Code